import qs from "qs";
import axios from "axios";
import { cloneDeep, isEmpty } from "lodash";
const { CancelToken } = axios;
window.cancelRequest = new Map();
axios.interceptors.request.use(
  (config) => {
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
// import { baseUrl } from "../services/serve";
export default function request(opt) {
  let url = opt.url;

  // url = url.includes("://") ? url : baseUrl + "/" + url;
  let method = opt.method;

  let { data } = opt;
  //  判断get post请求给不给data
  let options = {};
  if (method.toLocaleLowerCase() === "get") {
    options = { url, method, data };
  } else {
    options = { url, method, data };
  }
  let cloneData = data ? cloneDeep(data) : new Object();

  options.url =
    method.toLocaleLowerCase() === "get"
      ? `${url}${isEmpty(cloneData) ? "" : "?"}${qs.stringify(cloneData)}`
      : `${url}`;
  options.cancelToken = new CancelToken((cancel) => {
    window.cancelRequest.set(Symbol(Date.now()), {
      pathname: window.location.pathname,
      cancel,
    });
  });
  return axios(options)
    .then((response) => {
      const { statusText, status, data } = response;

      let result = {};
      if (typeof data === "object") {
        result = data;
        if (Array.isArray(data)) {
          result.list = data;
        }
      } else {
        result.data = data;
      }

      return Promise.resolve({
        success: true,
        message: statusText,
        statusCode: status,
        ...result,
      });
    })
    .catch((error) => {
      const { response, message } = error;
      if (String(message) === "cancel request") {
        return { success: true };
      }
      let msg;
      let statusCode;
      if (response && response instanceof Object) {
        const { data } = response;
        statusCode = response.status;
        msg = data.msg;
        switch (statusCode) {
          case 400:
            // 参数服务后台发送log接收 后期做跳转页面发送给后端
            console.log("=========错误请求url======后端服务接收日志==========");
            console.log(window.event.currentTarget.responseURL);
            console.log(
              "=========后端相应内容============后端服务接收日志=========="
            );
            console.log(data.message);
            break;
          case 401:
            console.log(window.event.currentTarget.responseURL);
            console.log(data.message);
            break;
          case 403:
            console.log(options);
            console.log(window.event.currentTarget.responseURL);
            console.log(data.message);

            break;
          case 404:
            console.log(window.event.currentTarget.responseURL);
            console.log(data.message);
            // reject(new Error("接口未找到"));
            break;
          case 500:
            throw new Error("I crashed!");
            // reject(new Error("服务器错误"));
            break;
          default:
            // reject(new Error("服务器2222错误"));
            return response;
        }
      } else {
        statusCode = 600;
        msg = error.message || "Network Error";
      }

      /* eslint-disable */
      return Promise.reject({ success: false, statusCode, message: msg });
    });
}
